<?php 

namespace app\common\model;
use think\Db;
use think\Model;
use think\model\concern\SoftDelete;

class Cars extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $autoWriteTimestamp = true;

    
    // 定义全局的查询范围
/*     protected function base($query)
    {
        $agency_id = session('admin.agency_id');
        if(strpos($agency_id, '1')===false){   
            
            $query->where('agency_id','in',$agency_id)->where('delete_time',null);
            
        }else{
            $query->where('delete_time',null);
        }
        
    }  */
    
    public function carsInfo()
    {
        return $this->hasOne('CarsInfo','car_unique_id','car_unique_id')->field('car_unique_id,machine_version,network_status,voltage,machine_report_time,time');
    }
    
    public function brand()
    {
        return $this->hasOne('Brand','id','brand_id')->field('id,name');
    }
    
    public function agency()
    {
        return $this->hasOne('Agency','id','agency_id')->field('id,name');
    }
    
    public function carsPark()
    {
        return $this->hasOne('carsPark','id','car_park')->field('id,name');
    }
    

    public function getHolderNameAttr($value,$data)
    {
        if($data['holder_type'] ==0){
            return Db::name('company')->where(['id'=>$data['holder_id']])->value('name');
        }else{
            return Db::name('personal')->where(['id'=>$data['holder_id']])->value('name');
        }
    }
}